﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System;
using System.IO;
using System.Windows.Media;

namespace Microsoft.Telco.Controls.Messaging.AudioProvider
{
 
    /// <summary>
    /// A sink class for recorded audio
    /// </summary>
    public class MemoryAudioSink : AudioSink
    {
        #region Private Fields

        // Memory sink to where record will be kept
        private MemoryStream audioMemoryStream;
        
        // Current format in which the sinks records the input audio.
        private AudioFormat audioCaptureFormat;

        #endregion

        /// <summary>
        /// Gets the memory stream
        /// </summary>
        public Stream BackingStream 
        {
            get { return audioMemoryStream; }
        }

        /// <summary>
        /// Gets the Audio Format for the captured audio
        /// </summary>
        public AudioFormat AudioCaptureFormat
        {
            get { return audioCaptureFormat; }
        }

        /// <summary>
        /// overrides method OncaptureStarted from base class AudioSink
        /// </summary>
        protected override void OnCaptureStarted()
        {
            audioMemoryStream = new MemoryStream(1024);
        }

        /// <summary>
        /// Overrides method OnCaptureStopped from base class AudioSink
        /// </summary>
        protected override void OnCaptureStopped()
        {

        }

        /// <summary>
        /// Overrides method  OnFormatChange from base class AudioSink
        /// </summary>
        /// <param name="audioFormat"></param>
        protected override void OnFormatChange(AudioFormat audioFormat)
        {
            if (audioFormat.WaveFormat != WaveFormatType.Pcm)
                throw new InvalidOperationException("MemoryAudioSink supports only PCM audio format.");

            audioCaptureFormat = audioFormat;
        }

        /// <summary>
        /// Overrides method OnSample from base class AudioSink
        /// </summary>
        /// <param name="sampleTime"></param>
        /// <param name="sampleDuration"></param>
        /// <param name="sampleData"></param>
        protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
        {
            // New audio data arrived, write them to the stream.
            audioMemoryStream.Write(sampleData, 0, sampleData.Length);
        }
    }
}
